# -*-coding: Utf-8 -*-
# @File : teacher_interface .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time：2024/5/7

from db import Teacher
from lib import Tools


class TeacherInterface(object):
    def __init__(self):
        self.tools = Tools()

    def login_interface(self, username, password):
        # 【一】获取所有的讲师信息名单
        flag, teacher_name_list = Teacher.show_name()
        # 【二】判断当前讲师是否存在
        if username not in teacher_name_list:
            info = self.tools.log_info(tag="warning", role="Teacher", username=username,
                                       info=f'登录失败!原因是当前讲师未注册!')
            return False, info
        # 【三】讲师存在允许登陆
        # 【1】获取当前讲师对象
        flag, teacher_obj = Teacher.select(name=username)
        # 【2】获取旧密码
        old_password = teacher_obj.password
        # 【3】获取新密码
        salt = teacher_obj.salt
        new_password = self.tools.encrypted_data(data=password, salt=salt)
        # 【4】校验密码是否一致
        if old_password != new_password:
            info = self.tools.log_info(tag="warning", role="Teacher", username=username,
                                       info=f'登录失败!原因是当前讲师密码错误!')
            return False, info
        # 【四】登陆成功
        info = self.tools.log_info(tag="info", role="Teacher", username=username, info=f'登录成功!')
        return True, info
